package cn.edu.gench.learn.day6;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/4/8.
 */
public class CommandParser {

    private static ArrayList<String>  commands = new ArrayList<String>();

    private static ArrayList<LibBook> books = new ArrayList<>();

    static {
        commands.add("login");
        commands.add("logout");
        commands.add("add");
        commands.add("del");
        commands.add("query");
        commands.add("borrow");
        commands.add("return");


        LibBook  libBook1 = new LibBook();
        libBook1.setName("自然");
        libBook1.setId("1000");

        LibBook  libBook2 = new LibBook();
        libBook2.setName("科学");
        libBook2.setId("2000");

        LibBook  libBook3 = new LibBook();
        libBook3.setName("计算机");
        libBook3.setId("3000");

        books.add(libBook1);
        books.add(libBook2);
        books.add(libBook3);
    }
    //"login,134455" = [login,124455]
    public boolean parse(String info){
        /**
         *  info = "query,all"
         *  infos = ["query","all"]
         *  command = "query"
         *  query 是不是在 commands
         *
         */
       String[] infos  = info.split(",");
       String command = infos[0];
       if(commands.contains(command)){
           doCommand(command,infos);
           return true;
       }else{
           return false;
       }
    }

    public void doCommand(String command,String[] infos){
        if("login".equals(command)) {
            System.out.println("login");
        }else if("logout".equals(command)){
            System.out.println("logout");
        }else if("add".equals(command)){
            System.out.println("add");
            addBook(infos);
        }else if("del".equals(command)){
            System.out.println("del");
            delBook(infos);
        }else if("query".equals(command)){
            System.out.println("query");
            query(infos);
        }else if("borrow".equals(command)){
            System.out.println("borrow");
            borrowBook(infos);
        }else if("return".equals(command)){
            System.out.println("return");
            returnBooke(infos);
        }
    }


    /**
     *
     *    borrow,134400
          return,134400
     *
     * @param infos
     */
    private void returnBooke(String[] infos) {
        String id = infos[1];
        for (LibBook one:books){
            if(one.getId().equals(id)){
                System.out.println(one.getName()+"书已经归还");
                one.setStatus(1);
            }
        }

        printBooks();
    }

    private void borrowBook(String[] infos) {
        String id = infos[1];
        for (LibBook one:books){
            if(one.getId().equals(id)){
                if(one.getStatus() == 0){
                    System.out.println(one.getName()+"书已经借出,不能重复借阅");
                }else{
                    System.out.println(one.getName()+"书已经借出");
                    one.setStatus(0);
                }

            }
        }
        printBooks();
    }

    private void delBook(String[] infos) {
        String id = infos[1];
        Book delBook  = null;
        for (Book one:books){
           if(one.getId().equals(id)){
               System.out.println("找到了"+ one.getName());
               delBook = one;

           }
        }
        if(delBook == null){
            System.out.println("没有找到");
        }else{
            books.remove(delBook);
        }

        printBooks();
    }

    //[0]add,[1]1455,[2]平行世界
    //
    public void addBook(String[] infos){
        String isadsadsadsd = infos[1];
        String fewrwerew = infos[2];
        LibBook book = new LibBook();
        book.setName(fewrwerew);
        book.setId(isadsadsadsd);
        books.add(book);
        printBooks();
    }


    public void printBooks(){
        for (LibBook one:books){
            /**
             *   书 一本书：所有书
             *
             */
            System.out.println(one);
        }
    }


    public void query(String[] infos){
        String id = infos[1];
        if(id.equals("all")){
            printBooks();
            return;
        }else{
            for (Book one:books){
                if(one.getId().equals(id)){
                    System.out.println(one);
                    return;
                }
            }
        }
        System.out.println("没有找到");
    }

}
